#ifndef SWUPDATECLIENT_H
#define SWUPDATECLIENT_H

#include <QObject>
#include <QLocalSocket>
#include <QTimer>
#include <QFileSystemWatcher>
#include <QFile>
#include <QFileInfo>
typedef enum {
    IDLE,
    START,
    RUN,
    SUCCESS,
    FAILURE,
    DOWNLOAD,
    DONE,
    SUBPROCESS,
    PROGRESS,
} RECOVERY_STATUS;

typedef enum {
    SOURCE_UNKNOWN,
    SOURCE_WEBSERVER,
    SOURCE_SURICATTA,
    SOURCE_DOWNLOADER,
    SOURCE_LOCAL
} sourcetype;
struct progress_msg {
    unsigned int	magic;		/* Magic Number */
    RECOVERY_STATUS	status;		/* Update Status (Running, Failure) */
    unsigned int	dwl_percent;	/* % downloaded data */
    unsigned int	nsteps;		/* No. total of steps */
    unsigned int	cur_step;	/* Current step index */
    unsigned int	cur_percent;	/* % in current step */
    char		cur_image[256];	/* Name of image to be installed */
    char		hnd_name[64];	/* Name of running hanler */
    sourcetype	source;		/* Interface that triggered the update */
    unsigned int 	infolen;    	/* Len of data valid in info */
    char		info[2048];   	/* additional information about install */
};

class SwUpdateClient : public QObject
{
    Q_OBJECT

public:
    explicit SwUpdateClient(QObject *parent = nullptr);
    ~SwUpdateClient();

    void start();
    void stop();

    void sendControlCommand(const QByteArray &cmd); // 向 control socket 发送命令

signals:
    void progressConnected();
    void progressDisconnected();
    void progressMessageReceived(QByteArray data);



private slots:
    void tryConnectProgress();


    void onProgressReadyRead();

    void onProgressError(QLocalSocket::LocalSocketError error);

    void onProgressStateChanged(QLocalSocket::LocalSocketState state);

    void directoryChanged(const QString &path);

private:
    QString m_progressSocketPath = "/tmp/swupdateprog";


    QLocalSocket *m_progressSocket = nullptr;


    QFileSystemWatcher swUpdateFileWatch;
    bool isOpen= false;
};

#endif // SWUPDATECLIENT_H
